package it.polimi.traveldream.daos;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public abstract class GenericDAO<T> {
	
	@PersistenceContext
	private EntityManager em;
	
	private Class<T> entityClass;

	public GenericDAO(Class<T> entityClass) {
		this.entityClass = entityClass;
	}
	
	public void save(T entity){
		em.persist(entity);
	}
	
	public void delete(T entity){
		T entityToBeRemoved = em.merge(entity);
		em.remove(entityToBeRemoved);	
	}
	
	public T update(T entity){
		return em.merge(entity);
	}
	
	public T find(int entityId){
		return em.find(entityClass, entityId);
	}
	
	

}